Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Listing 9.3 Code in COPYLINK.FRM.
    Option Explicit

    Private Sub Command1_Click()

    ‘ Clear the Picture Box.
    Picture1.picture = LoadPicture()

    End Sub

    Private Sub Command2_Click()

    ‘ Quit the program.
    End

    End Sub

    Private Sub mnuEdit_Click()

    ‘ Enable the Copy command only if the Picture Box
    ‘ or the Text Box is active.

    If TypeOf Screen.ActiveControl Is TextBox Or TypeOf Screen.ActiveControl _
        Is PictureBox Then
        mnuEditCopy.Enabled = True
    Else
        mnuEditCopy.Enabled = False
    End If

    End Sub

    Private Sub mnuEditCopy_Click()

    Dim LinkInfo As String

    ‘ Clear the Clipboard.
    Clipboard.Clear

    ‘ If the active control is the Text Box copy its text and
    ‘ DDE information to the Clipboard.
    If TypeOf Screen.ActiveControl Is TextBox Then
        LinkInfo = App.EXEName & “|” & LinkTopic
        LinkInfo = LinkInfo & “!” & Screen.ActiveControl.Tag
        Clipboard.SetText LinkInfo, vbCFLink
        Clipboard.SetText Screen.ActiveControl.TEXT
    End If

    ‘ If the active control is a Picture Box copy its picture and
    ‘ DDE information to the Clipboard.
    If TypeOf Screen.ActiveControl Is PictureBox Then
        LinkInfo = App.EXEName & “|” & LinkTopic
        LinkInfo = LinkInfo & “!” & Screen.ActiveControl.Tag
        Clipboard.SetText LinkInfo, vbCFLink
        Clipboard.SetData Screen.ActiveControl.picture
    End If

    End Sub
### More DDE Magic DDE offers a great deal more capability than is possible to cover here. However, I would like to finish this chapter with one more use for DDE—something I find very helpful. Using the power of DDE, we can employ Visual Basic to create extensions to existing programs, circumventing omissions or shortcomings in their design and capabilities. For example, we may like to use the Microsoft Excel spreadsheet program for manipulation and graphing of numerical data. And why not? Excel has a wide range of powerful features, and trying to duplicate even a few of them in a Visual Basic program would be a major programming challenge. In my opinion, however, Excel falls short in one area: data entry. Typing large amounts of data into the spreadsheet’s row-and-column structure can be an exercise in frustration and error. As you know, however, Visual Basic makes it easy to create attractive, easy-to-use forms for data entry and other purposes, and also to include data validation code to prevent entry of incorrect data. By using DDE, we can create a Visual Basic data-entry program that sends the data to an Excel spreadsheet. We get the best of both worlds: the convenience of a Visual Basic data-entry form and the analytical capabilities of Excel. Using DDE in this way is quite different from the uses we covered earlier in the chapter. DDE is used to transfer data, but no link is established. Once a piece of data is “sent” to Excel, we have no link back to the Visual Basic program—the end result is exactly the same as if the data had been typed directly into Excel. In addition to sending data to other programs, DDE can be used to send commands, instructing programs to perform various actions. You’ll see that several actions are required for this sort of application. The first action we will need is the ability to start a program, if it is not already running. To start another Windows application from within a Visual Basic program, you use the Shell function. The syntax is shown here:
    result = Shell(CommandString [, WindowStyle])
The CommandString argument specifies the name of the program to execute, including any required arguments or command-line switches. If the program name in CommandString doesn’t include a .COM, .EXE, .BAT, or .PIF file extension, .EXE is assumed. The WindowStyle argument specifies the style of the application window when the program starts, such as maximized or minimized. We can omit this argument, and the program is opened, minimized with focus. Table 9.2 shows the possible values for WindowStyle and the defined constants. If the application is successfully initiated, the Shell function returns the application’s task identification, a unique number that identifies the running application. If the Shell function is unable to start the named application, an error occurs. Table 9.2 Constants for the WindowStyle argument.
Constant | Value | Description
--- | --- | ---
vbHide | 0 | Window is hidden and focus is passed to the hidden window.
vbNormalFocus | 1 | Window has focus and is restored to its original size and position.
vbMinimizedFocus | 2 | Window is displayed as an icon with focus.
vbMaximizedFocus | 3 | Window is maximized with focus.
vbNormalNoFocus | 4 | Window is restored to its most recent size and position. The currently active window remains active.
vbMinimizedNoFocus | 6 | Window is displayed as an icon. The currently active window remains active.
When using the Shell function, remember that it runs other applications asynchronously—the Visual Basic program does not wait for the Shell command to complete. This means that we can’t be sure that a program started with Shell will have completed its start-up procedures before the code following the Shell function in our Visual Basic application is executed. #### Sending Commands To Another Program Once you have established a DDE link, you can use the link for more than transferring data. You can also use it to send commands to the other application using the LinkExecute method. This method is applied to the control that is maintaining the link—the active destination control. That’s just a syntax requirement, however, as the control has nothing to do with the command we are sending. The syntax for the LinkExecute method is shown here:
    ControlName.LinkExecute Command
ControlName is the name of a control that is the destination in an active DDE link. The Command argument is a string that contains the commands to be sent to the DDE source application. The legal commands depend on the specific application. In other words, no universal “DDE command set” exists that contains commands for use with all applications. You’ll need to refer to the documentation for each source application for information on the commands it accepts. Excel, for example, accepts any of its macro commands enclosed in brackets. #### Sending Data To Another Program Once an active DDE link is established, you can send data from the destination program to the source program. Note that this is the reverse of the more typical data flow in a DDE conversation, which is from source to destination. To send data from the destination to the source, use the LinkPoke method. The procedure is as follows: 1. Put the data to be transferred in a Text Box or Picture Box control. 2. Set the control’s LinkMode property to 0-None. 3. Set the control’s LinkTopic and LinkItem properties to identify the location where you want to poke the data. 4. Set the LinkMode property to 2-Manual. 5. Execute the LinkPoke method. For example, the following code will poke the text “Testing LinkPoke” to cell B2 in the Excel spreadsheet TESTDATA.XLS:
    Text1.Text = “Testing LinkPoke”
    Text1.LinkMode = 0
    Text1.LinkTopic = “EXCEL|TESTDATA”
    Text1.LinkItem = “R2C2”
    Text1.LinkMode = 2
    Text1.LinkPoke
Note the use of “R2C2” to refer to the second row in the second column of the Excel spreadsheet. For DDE commands, Excel uses numbers for both rows and columns rather than using letters for columns, as is the case when you are working directly in Excel.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.